<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        @click="switchTab(index)"
        :class="{ active: index === currentIndex }"
      >
        <div class="role-info">
          <svg-icon
            :icon-class="index === currentIndex ? 'user-active' : 'user'"
            class="icon"
          ></svg-icon>
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more"></svg-icon>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="goEdit(item.roleId)"
                >编辑角色</el-dropdown-item
              >
              <auth-btn btnPerm="sys:role:add_edit">
                <el-dropdown-item>添加角色</el-dropdown-item>
              </auth-btn>
              <el-dropdown-item
                @click.native="delRole(item.roleId)"
                v-btn-auth="'sys:role:remove'"
                >删除</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')"
        >添加角色</el-button
      >
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="first">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                node-key="id"
                :data="item.children"
                :props="defaultProps"
                show-checkbox
                default-expand-all
              ></el-tree>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="user">
          <div class="user-wrapper">
            <el-table :data="roleUserList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {
  getRoleListAPI,
  getTreeListAPI,
  getRoleDetailAPI,
  getRoleUserAPT,
  delRoleUserAPI,
} from "@/api/system";

// 递归添加disabled属性
function addDisables(treeList) {
  treeList.forEach((item) => {
    item.disabled = true;
    if (item.children) {
      addDisables(item.children);
    }
  });
}
export default {
  name: "Role",
  data() {
    return {
      roleList: [],
      currentIndex: 0,
      treeList: [],
      defaultProps: {
        children: "children",
        label: "title",
      },
      activeName: "first",
      roleUserList: [],
    };
  },
  async mounted() {
    await this.getRoleList();
    await this.getTreeList();
    // 组件初始化的时候模拟点击第一项
    // this.roleList不能为空  treeList也不能为空
    this.switchTab(0);
  },
  methods: {
    delRole(roleId) {
      this.$confirm("是否确认删除当前角色？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await delRoleUserAPI(roleId);
        this.getRoleList();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    goEdit(roleId) {
      // console.log("去编辑");
      this.$router.push({
        path: "/addRole",
        query: {
          roleId,
        },
      });
    },
    async getRoleList() {
      const res = await getRoleListAPI();
      this.roleList = res.data;
    },
    async getRoleUserList(roleId) {
      // 实现成员列表的接口调用
      const resMember = await getRoleUserAPT(roleId);
      this.roleUserList = resMember.data.rows;
    },
    async switchTab(index) {
      this.currentIndex = index;
      // 拿到当前角色id
      const roleId = this.roleList[index].roleId;
      // console.log(roleId);
      // 使用id获取高亮权限点列表
      const res = await getRoleDetailAPI(roleId);
      // console.log(res);
      const perms = res.data.perms;
      // console.log(this.$refs.tree);
      this.$refs.tree.forEach((treeInstance, index) => {
        treeInstance.setCheckedKeys(perms[index]);
      });

      this.getRoleUserList(roleId);
    },
    async getTreeList() {
      const res = await getTreeListAPI();
      this.treeList = res.data;
      addDisables(this.treeList);
    },
  },
};
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>